﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Models;
using Utils;
using WebUI.Utils;
using DomainModel;
using WebUI.HtmlHelpers;

namespace WebUI.Controllers
{
    public class MessageController : BaseController
    {
        [Authorize]
        public PartialViewResult New(string receiptID)
        {
            return PartialView(new MessageNewViewModel(receiptID));
        }

        [Authorize]
        [HttpPost]
        public JsonResult New(MessageNewViewModel model)
        {
            if (ModelState.IsValid)
            {
                //check if send recently
                EmailLog emailLog = DataUtils.GetOne<EmailLog>(ConstUtils.SP_GetOneEmailLogByUserName, new { SenderLoginID = IdentityWithoutUserName.LoginID, ReceiverUserName = model.ReceiptID});
                //if already sent recently
                if (ViewModelUtils.IsSentRecently(emailLog))
                {
                    return Json(JsonResultModelHelper.ForError("You already contacted this talent recently"));
                }
                else
                {
                    string message = model.Message;
                    string receiver = model.ReceiptID;
                    string sender = string.Empty;
                    string senderUrl = string.Empty;

                    string from = string.Empty;
                    string to = string.Empty;
                    string bcc = GlobalUtilities.GetInstance().Email_BCC;
                    int receiverLoginID = 0;
                    int senderLoginID = 0;

                    IndLogin receiverIndLogin = DataUtils.GetIndLoginByUserName(model.ReceiptID);
                    to = receiverIndLogin.Login.Email;
                    receiverLoginID = receiverIndLogin.Login.LoginID;

                    if (IdentityWithoutUserName.IsIndLoggedIn)
                    {
                        IndLogin senderIndLogin = DataUtils.GetIndLoginByID(IdentityWithoutUserName.LoginID);
                        sender = senderIndLogin.Ind.FullName;
                        senderUrl = Url.ActionAbsolute("Shots", "Ind", new{UserName = senderIndLogin.Login.UserName});
                        from = senderIndLogin.Login.Email;
                        senderLoginID = senderIndLogin.Login.LoginID;
                    }
                    else
                    {
                        CoLogin senderCoLogin = DataUtils.GetCoLoginByID(IdentityWithoutUserName.LoginID);
                        sender = senderCoLogin.Co.Name;
                        senderUrl = Url.ActionAbsolute("Detail", "Co", new { CoID = senderCoLogin.Co.CoID }); //todo:
                        from = senderCoLogin.Login.Email;
                        senderLoginID = senderCoLogin.Login.LoginID;
                    }
                    
                    if (model.CopyMessage)
                    {
                        bcc = bcc + ";" + from;
                    }
                    EmailResult result = EmailUtils.Send(from, to, EmailTemplateType.ContactInd,
                        new string[] {
                            receiver, sender, senderUrl, message
                        }, receiverLoginID, senderLoginID, bcc, true);
                    if (result.Success)
                    {
                        return Json(JsonResultModelHelper.ForSuccessWithSummaryMessage("Message was sent successfully"));
                    }
                    else
                    {
                        return Json(JsonResultModelHelper.ForError("Internal error occurs. Please try it again."));
                    }
                }
            }
            else
            {
                return Json(JsonResultModelHelper.ForError(ModelState));
            }
        }

        [Authorize]
        public PartialViewResult Show(string receiptID)
        {
            return PartialView(new MessageShowViewModel(receiptID));
        }
    }
}
